package com.test;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class ThreadDemo1 {
    private static int cnt = 0;

    public static void main(String[] args) {
        ExecutorService executorService = new ThreadPoolExecutor(5,10,2000,
                TimeUnit.MILLISECONDS,new ArrayBlockingQueue<>(100),
                new ThreadPoolExecutor.DiscardPolicy());
    }
    public static void main3(String[] args) {
        AtomicInteger atomicInteger = new AtomicInteger(0);
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while(true){
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(Thread.currentThread().getId()+"print atomicintteger:"+atomicInteger.incrementAndGet());
                }
            }
        };
        Thread t1 = new Thread(runnable);
        Thread t2 = new Thread(runnable);
        Thread t3 = new Thread(runnable);
        t1.start();
        t2.start();
        t3.start();
    }

    public static void main2(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum = 0;
                for (int i = 1; i <= 1000; i++) {
                    sum += i;
                }
                return sum;
            }
        };
        FutureTask<Integer> futureTask = new FutureTask<>(callable);
        Thread t = new Thread(futureTask);
        t.start();
        int res = futureTask.get();
        System.out.println(res);
    }
    public static void main1(String[] args) {
        Semaphore semaphore = new Semaphore(1);

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while(true) {
                    try {
                        semaphore.acquire();
                        System.out.println(Thread.currentThread().getId()+" print cnt:"+(++cnt));
                        semaphore.release();
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };
        Thread t1 = new Thread(runnable);
        Thread t2 = new Thread(runnable);
        t1.start();
        t2.start();
    }
}
